import wepy from 'wepy'
import tips from './tips'

export default class http {
    // get请求
    static async get(gateway, apiPath, data, loading = true) {
        let apiRs = await this.apiRequest('GET', gateway, apiPath, data, "application/json", loading);
        return this.bizExect(apiRs, apiPath)
    }

    // post表单请求
    static async postForm(gateway, apiPath, data, loading = true) {
        let apiRs = await this.apiRequest('POST', gateway, apiPath, data, "application/x-www-form-urlencoded", loading);
        return this.bizExect(apiRs, apiPath);
    }

    // postJson请求
    static async postJson(gateway, apiPath, data, loading = true) {
        let apiRs = await this.apiRequest('POST', gateway, apiPath, data, "application/json", loading);
        return this.bizExect(apiRs, apiPath);
    }

    // 审查httpcode
    static bizExect(apiRs, apiPath) {
        apiRs.ApiPath = apiPath;
        if (apiRs.domainerror) {
            apiRs.Next = false;
            tips.error("请求域名配置错误");
            return apiRs;
        }
        if (apiRs.HttpCode == 423) {
            apiRs.Next = false;
            return apiRs;
        }
        return apiRs
    }

    static async apiRequest(method, gateway, apiPath, data, contenType, loading = true) {
        if (wepy.$instance.globalData.stopReq) {
            return {
                HttpCode: 423,
                Next: true,
                Code: 423,
                Msg: "正在登录中，放弃当前请求！",
                Data: null
            };
        }

        let url = this.getApiUrl(gateway, apiPath);
        
        return await this.wxRequest(method, url, data, contenType, loading);
    }

    static getApiUrl(gateway, apiPath) {
        let baseUrl = wepy.$instance.globalData.ApiDomians;
        
        if (baseUrl[gateway]) {
            return `${baseUrl[gateway]}/${apiPath}`;
        }
        
        return baseUrl.default + '/' + apiPath;
    }

    static async wxRequest(method, url, data, contenType, loading = true) {
        const param = {
            url: url,
            method: method,
            data: data,
            header: this.GetWxaHeader(contenType)
        };
        if (loading) {
            tips.loading();
        }

        const wpRes = await wepy.request(param).catch(ex => {
            if (ex && ex.errMsg == "request:fail url not in domain list") {
                return {
                    statusCode: 500,
                    domainerror: true
                }
            } else if (ex && ex.errMsg && ex.errMsg.indexOf("request:fail") > -1) {
                console.log(ex, 506)
                return {
                    statusCode: 506,
                };
            } else {
                throw (ex);
            }
        });

        let res = wpRes.data;
        if (res) {
            res.HttpCode = wpRes.statusCode;
            res.Next = true;
        } else {
            res = {
                HttpCode: wpRes.statusCode,
                Code: wpRes.statusCode,
                Data: null,
                Next: true,
                domainerror: wpRes.domainerror
            };
        };
        return res;
    }

    // 公共请求头
    static GetWxaHeader(contenType) {
        let headerData = {
            "Content-Type": contenType, //"application/x-www-form-urlencoded"//application/json,
            "Xkd-From": "weapp",
            "Authorization": 'Bearer ' + wepy.$instance.globalData.jwtToken,
        };
        return headerData;
    }

}